<%@ page contentType="text/html;charset=UTF-8" %>
<script>
$(document).ready(function() {
    $.ajax({
        url : "${ctx}/ap/card/li",
        success : function(data) {
            $("#ykkh").empty();
            $("#ykkh").append("<option value=''>请选择</option>");
            $("#code1").empty();
            $("#code1").append("<option value=''>请选择</option>");
            $.each(JSON.parse(data), function (idx, item) {
                $("#ykkh").append("<option value='"+item.number+"'>"+item.number+"</option>");
                $("#code1").append("<option value='"+item.number+"'>"+item.number+"</option>");
            })
        },
        error : function(data){
            alert("操作失败!");
        }
    });
	$('#tmsWaybillTable').bootstrapTable({
		 
		  //请求方法
               method: 'get',height:550,
               //类型json
               dataType: "json",
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
               //显示 内容列下拉框
    	       showColumns: true,
    	       //显示到处按钮
    	       showExport: true,
    	       //显示切换分页按钮
    	       showPaginationSwitch: true,
    	       //显示详情按钮
    	       detailView: true,
    	       	//显示详细内容函数
	           detailFormatter: "detailFormatter",
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）     
               cache: false,    
               //是否显示分页（*）  
               pagination: true,   
                //排序方式 
               sortOrder: "asc",  
               //初始化加载第一页，默认第一页
               pageNumber:1,   
               //每页的记录行数（*）   
               pageSize: 10,  
               //可供选择的每页的行数（*）    
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据  
               url: "${ctx}/tms/tmsWaybill/data1",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',   
               ////查询参数,每次调用是会带上这个参数，可自定义                         
               queryParams : function(params) {
               	var searchParam = $("#searchForm").serializeJSON();
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                   	edit(row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该运单记录吗？', function(){
                       	jp.loading();
                       	jp.get("${ctx}/tms/tmsWaybill/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#tmsWaybillTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})
                   	   
                   	});
                      
                   } 
               },
              
               onClickRow: function(row, $el){
               },
               columns: [{
		        checkbox: true
		       
		    }


                   ,{
		        field: 'yfk',
		        title: '预付款',
		        sortable: true
		       
		    }
                   ,{
                       field: 'hzjs',
                       title: '回执结算',
                       sortable: true

                   }       ,{
                       field: 'shipmentNumbers',
                       title: '装运单号',
                       sortable: true

                   }  ,{
                       field: 'weight',
                       title: '总吨位',
                       sortable: true

                   }
                   ,{
                       field: 'contractDistance',
                       title: '起运地',
                       sortable: true

                   }

                   ,{
                       field: 'code',
                       title: '合同号',
                       sortable: true

                   }
			,{
		        field: 'forwardingPrice',
		        title: '租车协议号',
		        sortable: true
		       
		    } ,{
                       field: 'loadingPayment',
                       title: '预付款',
                       sortable: true

                   }
                   ,{
                       field: 'loadingJe',
                       title: '预付油卡',
                       sortable: true

                   }
                   ,{
                       field: 'kdPayment',
                       title: '货到合同金额',
                       sortable: true

                   }
                   ,{
                       field: 'receiptCalculate',
                       title: '回执合同金额',
                       sortable: true

                   }
                   ,{
                       field: 'rentingCost',
                       title: '租车费用',
                       sortable: true

                   }

                   ,{
                       field: 'commonCarrier',
                       title: '承运商',
                       sortable: true

                   }
		    ,{
                       field: 'payeeName',
                       title: '收款人姓名',
                       sortable: true

                   } ,{
                       field: 'cardNumber',
                       title: '卡号',
                       sortable: true

                   } ,{
                       field: 'bankAddress',
                       title: '开户行',
                       sortable: true

                   }
                   ,{
                       field: 'driverName',
                       title: '司机姓名',
                       sortable: true

                   }
                   ,{
                       field: 'plateNumber',
                       title: '车牌号码',
                       sortable: true

                   },{
                       field: 'arriveCity',
                       title: '到站地',
                       sortable: true

                   }


		     ]
		
		});
		
		  
	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端

		 
		  $('#tmsWaybillTable').bootstrapTable("toggleView");
		}
	  
	  $('#tmsWaybillTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#jf').prop('disabled',  $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
            $('#hz').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#jf1').prop('disabled',  $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#hz1').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#bx').prop('disabled',  $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#bxqr').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);

          $('#add').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#kjlc').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#e').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#dayin').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);
          $('#day').prop('disabled', $('#tmsWaybillTable').bootstrapTable('getSelections').length!=1);

          $('#del').prop('disabled', ! $('#tmsWaybillTable').bootstrapTable('getSelections').length);
        });
		  
		$("#btnImport").click(function(){
			jp.open({
			    type: 1, 
			    area: [500, 300],
			    title:"导入数据",
			    content:$("#importBox").html() ,
			    btn: ['下载模板','确定', '关闭'],
				    btn1: function(index, layero){
					  window.location='${ctx}/tms/tmsWaybill/import/template';
				  },
			    btn2: function(index, layero){
				        var inputForm =top.$("#importForm");
				        var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe 
				        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
				        inputForm.onsubmit = function(){
				        	jp.loading('  正在导入，请稍等...');
				        }
				        inputForm.submit();
					    jp.close(index);
				  },
				 
				  btn3: function(index){ 
					  jp.close(index);
	    	       }
			}); 
		});
		    
	  $("#search").click("click", function() {// 绑定查询按扭
		  $('#tmsWaybillTable').bootstrapTable('refresh');
		});
	 
	 $("#reset").click("click", function() {// 绑定查询按扭
		  $("#searchForm  input").val("");
		  $("#searchForm  select").val("");
		   $("#searchForm  .select-item").html("");
		  $('#tmsWaybillTable').bootstrapTable('refresh');
		});
		
		
	});
function yfkqr() {
    var yfk=getyfk();
    if(yfk!="预处理"){
        return;
    }
    $("#skr3").val(getpayeeName());
    $("#kh3").val(getcardNumber());
    $("#khh3").val(getbankAddress());
    var yf=getyf();
    var yfyk=getyfyk();
    if(yfyk==""){
        yfyk="0";
    }
    $("#yfyk3").val(yfyk);
    $("#yfzje3").val(yf);
    $("#yfxj3").val(parseFloat(yf)-parseFloat(yfyk));
    $.ajax({
		url:"${ctx}/tms/tmsWaybill/getdetil?i=" + getIdSelections(),
		dataType:"Json",
		success:function (data) {
            $("#ykkh3").val(data.number);
            $("#sfxj3").val(data.sfxj);
            $("#sfyk3").val(data.sfyk);
            $("#sfzje").val(data.sfzje);
            $("#remark3").val(data.remarks);
        }
	})
    $("#yfqr").modal("show");
}


function qdtb() {
    $("#qyd1").val("");
    $("#mdd1").val("");
    $("#zl1").val("");
    $("#gsje1").val("")
    $("#tbqr").modal("hide");
    jp.get("${ctx}/tms/tmsWaybill/qdtb?id=" + getIdSelections(), function(data){
        if(data.success){
            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success("确认投保成功");
        }else{
            jp.error(data.msg);
        }
    })

}


function qd3() {
    $("#yfqr").modal("hide");
    jp.get("${ctx}/tms/tmsWaybill/yfkqd?i=" + getIdSelections(), function(data){
        if(data.success){

            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success("预付款确定成功");
        }else{
            jp.error(data.msg);
        }
    })
	
}

function qx3() {
    $("#yfqr").modal("hide");
    jp.get("${ctx}/tms/tmsWaybill/yfkqx?i=" + getIdSelections(), function(data){
        if(data.success){

            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success("预付款取消成功");
        }else{
            jp.error(data.msg);
        }
    })

}
function qd4() {
    $("#hzqr").modal("hide");
    jp.get("${ctx}/tms/tmsWaybill/hzqd?i=" + getIdSelections(), function(data){
        if(data.success){

            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success("回执付款确定成功");
        }else{
            jp.error(data.msg);
        }
    })

}

function qx4() {
    $("#hzqr").modal("hide");
    jp.get("${ctx}/tms/tmsWaybill/hzqx?i=" + getIdSelections(), function(data){
        if(data.success){

            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success("回执款取消成功");
        }else{
            jp.error(data.msg);
        }
    })

}
function qd1() {
    var ykkh=$("#ykkh").val();
    var sfxj=$("#sfxj").val();
    var sfyk=$("#sfyk").val();
    var remark=$("#remark1").val();
    if(sfyk==""){
        jp.warning("油卡金额不能为空");
        return;
    }
    if(sfxj==""){
        jp.warning("现金金额不能为空");
        return;
    }

    jp.get("${ctx}/tms/tmsWaybill/yfk?i=" + getIdSelections()+"&xj="+sfxj+"&yk="+sfyk+"&kh="+ykkh+"&sm="+remark, function(data){
        if(data.success){
            $("#myModa2").modal("hide");
            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success(data.msg);
        }else{
            jp.error(data.msg);
        }
    })

}
function yfk() {
var yfk=getyfk();
if(yfk!="未过账"){
	return;
}
    $("#ykkh").val("");
   $("#sfxj").val("");
    $("#sfyk").val("");
   $("#remark1").val("");
    $("#skr1").val(getpayeeName());
    $("#kh1").val(getcardNumber());
    $("#khh1").val(getbankAddress());
    var yf=getyf();
    var yfyk=getyfyk();
    if(yfyk==""){
    	yfyk="0";
	}
    $("#yfyk").val(yfyk);
    $("#yfzje").val(yf);
    $("#yfxj").val(parseFloat(yf)-parseFloat(yfyk));
    $("#myModa2").modal("show");

}
function hzfk() {
    var hzfk=gethzfk();
    if(hzfk!="未过账"){
        return;
    }
    $("#yk").val("");
   $("#xj").val("");
    $("#hdxj").val("");
    $("#remark").val("");
	var hz=gethz();
	var zc=getzc();
    $("#skr").val(getpayeeName());
    $("#kh").val(getcardNumber());
    $("#khh").val(getbankAddress());
	$("#hzht").val(hz);
    $("#hzxy").val(zc);
    $("#hdfk").val(gethd());
    $("#hzzje").val(parseFloat(hz)+parseFloat(zc));
    $("#myModal").modal("show");


}
function hzqr() {
    var hzfk=gethzfk();
    if(hzfk!="预处理"){
        return;
    }
    var hz=gethz();
    var zc=getzc();
    $("#skr4").val(getpayeeName());
    $("#kh4").val(getcardNumber());
    $("#khh4").val(getbankAddress());
    $("#hzht4").val(hz);
    $("#hzxy4").val(zc);
    $("#hdfk4").val(gethd());
    $("#hzzje4").val(parseFloat(hz)+parseFloat(zc));
    $.ajax({
        url:"${ctx}/tms/tmsWaybill/getdetil0?i=" + getIdSelections(),
        dataType:"Json",
        success:function (data) {
            $("#yk4").val(data.oil);
            $("#xj4").val(data.money);
            $("#hdxj4").val(data.sfhd);
            $("#remark4").val(data.remarks);
            $("#code4").val(data.number);
            $("#sfzje1").val(data.sfzje);
        }
    })
    $("#hzqr").modal("show");
}
function qd6() {
	var yk=$("#yk").val();
	var xj=$("#xj").val();
	var hd=$("#hdxj").val();
	if(yk==""){
	    jp.warning("油卡金额不能为空");
	    return;
    }
    if(xj==""){
        jp.warning("现金金额不能为空");
        return;
    }
    if(hd==""){
        jp.warning("货到金额不能为空");
        return;
    }
	var remark=$("#remark").val();

	var number=$("#code1").val();
    jp.get("${ctx}/tms/tmsWaybill/hzfk?i=" + getIdSelections()+"&yk="+yk+"&xj="+xj+"&number="+number+"&hd="+hd+"&sm="+remark, function(data){
        if(data.success){
            $("#myModal").modal("hide");
            $('#tmsWaybillTable').bootstrapTable('refresh');
            jp.success(data.msg);
        }else{
            jp.error(data.msg);
        }
    })


}
  function getIdSelections() {
        return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }


function gethz() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.receiptCalculate
    });}
function geqsd() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.contractDistance
    });}


function gedzd() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.arrive
    });}
function gezl() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.weight
    });}
function getyfk() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.yfk
    });}
function gethzfk() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.hzjs
    });}
function getyf() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.loadingPayment
    });}
function getyfyk() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.loadingJe
    });}
function gethd() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.kdPayment
    });}
function getpayeeName() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.payeeName
    });}
function getcardNumber() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.cardNumber
    });}
function getbankAddress() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.bankAddress
    });}
function getzc() {
    return $.map($("#tmsWaybillTable").bootstrapTable('getSelections'), function (row) {
        return row.rentingCost
    });}

  function deleteAll(){

		jp.confirm('确认要删除该运单记录吗？', function(){
			jp.loading();  	
			jp.get("${ctx}/tms/tmsWaybill/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#tmsWaybillTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})
          	   
		})
  }




  
  function edit(id){//没有权限时，不显示确定按钮
  	  if(id == undefined){
			id = getIdSelections();
		}
	   <shiro:hasPermission name="tms:tmsWaybill:edit">
	  jp.openDialogView('查看运单', "${ctx}/tms/tmsWaybill/form?id=" + id,'1000px', '800px', $('#tmsWaybillTable'));
	   </shiro:hasPermission>
	  <shiro:lacksPermission name="tms:tmsWaybill:edit">
	  jp.openDialogView('查看运单', "${ctx}/tms/tmsWaybill/form?id=" + id,'800px', '500px', $('#tmsWaybillTable'));
	  </shiro:lacksPermission>
  }
  
  
  
  
		   
  function detailFormatter(index, row) {
	  var htmltpl =  $("#tmsWaybillChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
	  var html = Mustache.render(htmltpl, {
			idx:row.id
		});
	  $.get("${ctx}/tms/tmsWaybill/detail?id="+row.id, function(tmsWaybill){
    	var tmsWaybillChild1RowIdx = 0, tmsWaybillChild1Tpl = $("#tmsWaybillChild1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data1 =  tmsWaybill.tmsMaterialsInformationList;
		for (var i=0; i<data1.length; i++){
			addRow('#tmsWaybillChild-'+row.id+'-1-List', tmsWaybillChild1RowIdx, tmsWaybillChild1Tpl, data1[i]);
			tmsWaybillChild1RowIdx = tmsWaybillChild1RowIdx + 1;
		}
				
    	var tmsWaybillChild2RowIdx = 0, tmsWaybillChild2Tpl = $("#tmsWaybillChild2Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data2 =  tmsWaybill.tmsAdditionalChargeList;
		for (var i=0; i<data2.length; i++){
			addRow('#tmsWaybillChild-'+row.id+'-2-List', tmsWaybillChild2RowIdx, tmsWaybillChild2Tpl, data2[i]);
			tmsWaybillChild2RowIdx = tmsWaybillChild2RowIdx + 1;
		}
				
      	  			
      })
     
        return html;
    }
  
	function addRow(list, idx, tpl, row){
		$(list).append(Mustache.render(tpl, {
			idx: idx, delBtn: true, row: row
		}));
	}

			
</script>
<script type="text/template" id="tmsWaybillChildrenTpl">//<!--
	<div class="tabs-container">
		<ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-{{idx}}-1" aria-expanded="true">运单物料信息</a></li>
				<li><a data-toggle="tab" href="#tab-{{idx}}-2" aria-expanded="true">运单附加费用</a></li>
		</ul>
		<div class="tab-content">
				 <div id="tab-{{idx}}-2" class="tab-pane fade ">
						<table class="ani table">
						<thead>
							<tr>
								<th>支出类型</th>
								<th>金额</th>
								<th>数量</th>
								<th>单价</th>
								<th>备注信息</th>
							</tr>
						</thead>
						<tbody id="tmsWaybillChild-{{idx}}-2-List">
						</tbody>
					</table>
				</div>
				<div id="tab-{{idx}}-1" class="tab-pane fade in active">
					<table class="ani table">
						<thead>
							<tr>
								<th>提货单号</th>
								<th>物料编码</th>
								<th>物料名称</th>
								<th>物料规格</th>
								<th>派车量</th>
								<th>装车量</th>
								<th>毛重</th>
								<th>体积</th>
								<th>提货仓库</th>
								<th>备注信息</th>
							</tr>
						</thead>
						<tbody id="tmsWaybillChild-{{idx}}-1-List">
						</tbody>
					</table>
				</div>
		</div>//-->
	</script>
	<script type="text/template" id="tmsWaybillChild2Tpl">//<!--
				<tr>
					<td>
						{{row.balanceType}}
					</td>
					<td>
						{{row.money}}
					</td>
					<td>
						{{row.quantity}}
					</td>
					<td>
						{{row.unitPrice}}
					</td>
					<td>
						{{row.remarks}}
					</td>
				</tr>//-->
	</script>
	<script type="text/template" id="tmsWaybillChild1Tpl">//<!--
				<tr>
					<td>
						{{row.number}}
					</td>
					<td>
						{{row.coding}}
					</td>
					<td>
						{{row.name.name}}
					</td>
					<td>
						{{row.specification}}
					</td>

					<td>
						{{row.vehicleQuantity}}
					</td>
					<td>
						{{row.loadingQuantity}}
					</td>
					<td>
						{{row.weight}}
					</td>
					<td>
						{{row.volume}}
					</td>
					<td>
						{{row.depot}}
					</td>
					<td>
						{{row.remarks}}
					</td>
				</tr>//-->
	</script>
